<?php

namespace App\Http\Controllers\Auto;

use App\Http\Controllers\Controller;
use App\Models\AsyncEventModel;
use App\Models\GoodsPicModel;
use App\Models\GoodsPicPrimaryModel;
use App\Models\OrderGoodsModel;
use App\Models\SpecModel;
use App\Models\SupplyModel;
use App\Models\TriggerLogModel;
use App\Utils\ApiResponse;
use Illuminate\Http\JsonResponse;

class AutoAsyncEventController extends Controller
{
    use ApiResponse;

    /**
     * Rds数据库执行异步事件
     * @return JsonResponse
     */
    public function execAsyncEvent(): JsonResponse
    {
        $item = AsyncEventModel::where('finish_status', 0)->first();
        if ($item) {
            if ($item->event === 'inc')
                $this->handlePicPrimaryBuyIncEvent($item->table, $item->pid);
            $item->finish_status = 1;
            $item->save();
        }
        return $this->success($item);
    }

    /**
     * 触发器执行异步事件
     * @return JsonResponse
     */
    public function execTriggerEvent(): JsonResponse
    {
        $item = TriggerLogModel::where('finish_status', 0)->first();
        if ($item) {
            if ($item->modify_pid)
                $this->handlePicPrimarySidModifyEvent($item->modify_pid);
            if ($item->modify_sid)
                $this->handleSupplyModifyEvent($item->modify_sid);
            $item->finish_status = 1;
            $item->save();
        }
        return $this->success($item);
    }

    /**
     * 商品下载数自增
     * @param $table
     * @param $key
     * @return void
     */
    private function handlePicPrimaryBuyIncEvent($table, $key): void
    {
        if ($table === 'pic')
            GoodsPicModel::where('ID', $key)->orWhere('PID', $key)->increment('BuyNum');
    }

    /**
     * 商品商家编号修改响应
     * @param $pid
     * @return void
     */
    private function handlePicPrimarySidModifyEvent($pid): void
    {
        $sid = SpecModel::where('goods_id', $pid)->value('sid');
        $modifySid = GoodsPicPrimaryModel::where('ID', $pid)->value('SID');
        if ($sid && $modifySid && $sid !== $modifySid) {
            $supply = SupplyModel::where('ID', $modifySid)->select(['ID', 'CName', 'building', 'floor', 'room'])->first();
            SpecModel::where('goods_id', $pid)->update(['sid' => $modifySid, 'jst' => 0]);
            if ($supply)
                OrderGoodsModel::where('goods_id', $pid)->update([
                    'sid'       => $modifySid,
                    'sname'     => $supply->CName,
                    'building'  => $supply->building,
                    'floor'     => $supply->floor,
                    'room'      => $supply->room]);
        }
    }

    /**
     * 供应商地址修改响应
     * @param $sid
     * @return void
     */
    private function handleSupplyModifyEvent($sid): void
    {
        $supply = SupplyModel::where('ID', $sid)->select(['ID', 'CName', 'building', 'floor', 'room'])->first();
        if ($supply) {
            SpecModel::where('sid', $sid)->update(['jst' => 0]);
            OrderGoodsModel::where('sid', $sid)->update([
                'sid'       => $sid,
                'sname'     => $supply->CName,
                'building'  => $supply->building,
                'floor'     => $supply->floor,
                'room'      => $supply->room]);
        }
    }
}
